package com.ss.ttvideoengine.c;

import com.ss.ttvideoengine.d.TTVideoError;

import java.lang.ref.WeakReference;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

public final class f extends a {
    final class f$a implements Runnable {
        private final WeakReference<f> fRef;

        public f$a(f fVar) {
            this.fRef = new WeakReference(fVar);
        }

        public final void run() {
            f fVar = this.fRef.get();
            if (fVar != null && !fVar.g) {
                fVar.interrupt();
                fVar.onError(new TTVideoError("kTTVideoErrorDomainLocalDNS", -10000));
            }
        }
    }

    InetAddress f;
    boolean g;
    private Thread mThread;

    public f(String arg2) {
        super(arg2);
        this.g = false;
    }

    public final void b() {
        try {
            this.mThread = new Thread(new Runnable() {
                @Override
                public final void run() {
                    try {
                        f.this.f = InetAddress.getByName(f.this.c);
                    } catch (UnknownHostException unknownHostException) {
                        unknownHostException.printStackTrace();
                        f.this.g = true;
                        f.this.onError(new TTVideoError("kTTVideoErrorDomainLocalDNS", 0, unknownHostException.getMessage()));
                        return;
                    }

                    f.this.g = true;
                    if (f.this.f == null) {
                        f.this.onError(new TTVideoError("kTTVideoErrorDomainLocalDNS", -9998));
                        return;
                    }

                    String hostAddress = f.this.f.getHostAddress();
                    if ((f.this.f instanceof Inet6Address)) {
                        hostAddress = String.format("[%s]", hostAddress);
                    }
                    f.this.a(hostAddress);
                }
            });
            this.mThread.start();
        } catch (Exception exception) {
            this.onError(new TTVideoError("kTTVideoErrorDomainLocalDNS", 0, exception.getMessage()));
        }

        this.mHandler.postDelayed(new f$a(this), 10000);
    }

    public final void interrupt() {
        if (!this.isThreadInterrupted) {
            this.isThreadInterrupted = true;
            if (this.mThread == null) {
                return;
            }

            try {
                this.mThread.interrupt();
                this.mThread = null;
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    public final void release() {
        if (this.mThread != null) {
            this.mThread = null;
        }
    }
}

